package com.zhl.media.app.mvp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.utils.LogUtils;
import com.zhl.media.app.R;
import com.zhl.media.app.model.Building;
import com.zhl.media.app.model.Project;
import com.zhl.media.commonres.view.CustomExpandableListView;

import java.util.List;


public class BuildingAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<Building> groupDataList;

    public BuildingAdapter(Context context, List<Building> groupDataList) {
        mContext = context;
        this.groupDataList = groupDataList;
    }

    @Override
    public int getGroupCount() {
        return groupDataList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupDataList.get(groupPosition).getUnits().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupDataList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupDataList.get(groupPosition).getUnits().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.building_item,null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.tvTitle.setText(groupDataList.get(groupPosition).getName());
        //设置指示器
        if (isExpanded) {
            holder.ivArrow.setImageResource(R.drawable.down);
        } else {
            holder.ivArrow.setImageResource(R.drawable.left);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        UnitHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.unit_item,null);
            holder = new UnitHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (UnitHolder) convertView.getTag();
        }
        holder.name.setText(groupDataList.get(groupPosition).getUnits().get(childPosition).getName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    static class GroupHolder {
        private TextView tvTitle;
        private ImageView ivArrow;
        public GroupHolder(View v) {
            tvTitle = (TextView) v.findViewById(R.id.name);
            ivArrow = (ImageView) v.findViewById(R.id.iv_arrow);
        }
    }

    static class UnitHolder {
        private TextView name;
        public UnitHolder(View v) {
            name = (TextView) v.findViewById(R.id.name);
        }
    }

}